package com.ruiboyun.liveapp.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ruiboyun.liveapp.R;
import com.ruiboyun.liveapp.base.Url;
import com.ruiboyun.liveapp.bean.UpdatePortEvent;
import com.ruiboyun.liveapp.util.tool.EditorShare;
import com.ruiboyun.liveapp.util.tool.ToasUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdatePortActivity extends AppCompatActivity implements TextWatcher {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.et_port)
    EditText etPort;
    @Bind(R.id.save)
    Button save;
    private String port;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_port);
        ButterKnife.bind(this);

        port = EditorShare.getString(Url.PORT);
        etPort.setText(port);
        etPort.addTextChangedListener(this);
    }

    @OnClick({R.id.back, R.id.save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.save:
                String trim = etPort.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    ToasUtil.showCenterToast("端口号不能为空");
                } else {
                    if (!port.equals(trim)) {
                        EditorShare.putString(Url.PORT, trim);
                        EventBus.getDefault().post(new UpdatePortEvent(trim));
                    }

                    ToasUtil.showCenterToast("修改成功");
                    finish();

                }
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(s.toString().trim())) {
            save.setEnabled(false);
        } else {
            save.setEnabled(true);
        }
    }
}
